package queue;

public class MyQueue {

    static class ListNode{
        private int val;
        private ListNode prev;
        private ListNode next;

        public ListNode(int x){
            this.val = x;
        }
    }

    private ListNode front;
    private ListNode rear;
    private int usedSize;

    public boolean offer(int x){
        ListNode node = new ListNode(x);
        if(isEmpty()){
            front = rear = node;
        }else {
            node.next = rear;
            rear.prev = node;
            rear = node;
        }
        usedSize++;
        return true;
    }

    public int poll(){
        if(isEmpty()){
            return -1;
        }
        int val = front.val;
        front = front.prev;
        if(front != null){
            front.next = null;
        }
        usedSize--;
        return val;
    }

    public int peek(){
        return front.val;
    }




    public boolean isEmpty(){
        return usedSize == 0;
    }


}
